/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import com.jme3.scene.Node;


/**
 * Draws a tab button in the GUI.
 */
public class GuiTabButton extends Node implements IMouseOverable, IClickable {
	
	
	/**
	 * Label printed on the button.
	 */
	private String label;
	
	
	/**
	 * Offset of the coordinate system of this tab button.
	 */
	private GUI.Offset offset;
	
	
	/**
	 * Position of the tab button in the x axis. (Assuming the window has a width of 1024 pixel.)
	 */
	private int positionX;
	
	
	/**
	 * Position of the tab button in the y axis. (Assuming the window has a height of 768 pixel.)
	 */
	private int positionY;
	
	
	/**
	 * Z-Index of the element. An element with a higher index will cover an element with a lower one.
	 */
	private int zIndex;
	
	
	/**
	 * geometry element of the tab button.
	 */
	GuiQuad button;
	
	
	/**
	 * Label of the tab button.
	 */
	private GuiText text;
	
	
	/**
	 * Callback function which is called when tab button is pressed.
	 */
	private ICallback action;
	
	
	/**
	 * True if tab button is active. If the tab button is not active, the button will be unresponsive.
	 */
	private boolean active = true;
	
	
	/**
	 * Constructor
	 */
	public GuiTabButton(String label) {
		this.label = label;
	}
	
	
	/**
	 * Sets position and size
	 */
	public void setPositionSize(GUI.Offset offset, int positionX, int positionY, int zIndex) {
		this.offset = offset;
		this.positionX = positionX;
		this.positionY = positionY;
		this.zIndex = zIndex;
	}
	
	
	/**
	 * Set callback function which is called when button is pressed.
	 */
	public void setAction(ICallback action) {
		this.action = action;
	}
	
	
	/**
	 * Creates mesh and geometry and returns element;
	 */
	public GuiTabButton create() {		
		
		button = new GuiQuad();
		button.setAbsolutePositionSize(-10, -10, 160, 60, 0);
		button.setMaterial(MaterialManager.getTabButton(), 0.0f, 0.33333f, 1.0f, 0.33333f);
		button.create();
		
		button.setLocalTranslation(GUI.calculateX(offset, positionX), GUI.calculateY(offset, positionY), (float)zIndex);
		
        this.attachChild(button);
        
		text = new GuiText(label, Fonts.getNormal());
		text.setPositionSize(GUI.Offset.WINDOW, (140 - (int)text.getLineWidth()) / 2, 778, 1);
		
		text.setLocalTranslation(GUI.calculateX(offset, positionX), GUI.calculateY(offset, positionY), (float)zIndex);
        
		this.attachChild(text);
		
		return this;
	}
	
	
	/**
	 * Changes textures to show default button.
	 */
	private void showMouseOutTextures() {
		button.changeTextureCoords(0.0f, 0.33333f, 1.0f, 0.33333f);
	}
	
	
	/**
	 * Changes textures to show mouseOver button.
	 */
	private void showMouseOverTextures() {
		button.changeTextureCoords(0.0f, 0.66666f, 1.0f, 0.33333f);
	}
	
	
	/**
	 * Changes textures to show clicked button.
	 */
	private void showMouseDownTextures() {
		button.changeTextureCoords(0.0f, 0.66666f, 1.0f, 0.33333f);
	}
	
	
	/**
	 * Changes textures to show clicked button.
	 */
	private void showDeactivatedTextures() {
		button.changeTextureCoords(0.0f, 0.0f, 1.0f, 0.33333f);
	}
	
	
	/**
	 * Actives or deactives the button. If the button is deactivated, it will still be desplayed, but will be grey and unresponsive.
	 */
	public void setActive(boolean active) {
		this.active = active;
		if(active) {
			showMouseOutTextures();
		} else {
			showDeactivatedTextures();
		}
	}
	
	
	@Override
	public GUI.Offset getOffset() {
		return offset;
	}


	@Override
	public int getPositionX() {
		return positionX;
	}


	@Override
	public int getPositionY() {
		return positionY;
	}


	@Override
	public int getWidth() {
		return 140;
	}


	@Override
	public int getHeight() {
		return 40;
	}


	@Override
	public void onMouseOver() {
		if(active) {
			showMouseOverTextures();
		}
	}


	@Override
	public void onMouseOut() {
		if(active) {
			showMouseOutTextures();
		}
	}
	
	
	@Override
	public void onMouseDown(int mouseX, int mouseY) {
		if(active) {
			showMouseDownTextures();
		}
	}


	@Override
	public void onMouseUp(int mouseX, int mouseY) {
		if(active) {
			action.run();
		}		
	}
}
